/*
 * Copyright (C) 2000-2007 Wang Pengcheng <wpc0000@gmail.com>
 * Licensed to the Wang Pengcheng under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The LGPL licenses this file to You under the GNU Lesser General Public
 * Licence, Version 2.0  (the "License"); you may not use this file except in
 * compliance with the License.  You may obtain a copy of the License at
 *
 *     http://www.gnu.org/licenses/lgpl.txt
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
//28 Aug 2007
package cn.edu.whu.control;

import java.util.*;
import java.io.*;

import cn.edu.whu.data.DataList;
import cn.edu.whu.data.Data;
import cn.edu.whu.data.VarList;

/**
 * Get the basic information about the template and save them to the set.
 * DataList list and VarList var
 * @author wpc
 * @version 0.0.1
 *
 */

public class GetInfor {
	/**
	 * Save all the fields 
	 */
	private DataList list;
	private VarList var;
	private Scanner in = new Scanner(System.in);
	
	public GetInfor(DataList list,VarList var) throws FileNotFoundException{
		this.list=list;
		this.var=var;
		
	}
	
	public void setInput(InputStream s){
		in = new Scanner(s);
				//new FileInputStream(
		//"src/cn/edu/whu/control/test.txt"));
	}
	
	/**
	 * Get all information
	 */
	public void get(){
		ask();
		set();
	}
	
	/**
	 * Give the information to the user 
	 * And get the informations.
	 */
	public void ask(){
		//Get the information of the class
		//The first line is the package
		//The second line is the class-name
		for(int i=0;i<2;i++){
			System.out.print(var.getVar(i)+"=");
			var.setValue(i, in.next());
		}
		//Set the s is the basic name of the javabean class
		String s = var.getValue("$DATA");
		//In order to match the DATABASE name, it will change the basic name to the upper case
		//s=s.toUpperCase();
		//TODO set the database table name
		s = s.toLowerCase();
		var.setValue("$DBTABLENAME", s);
		
		System.out.print("The attribute's number is:");
		//Get the attribute's number of the javabean class
		int k=in.nextInt();
		for(int i=0;i<k;i++){
			System.out.print("Enter the information about an attribute[type name only]:");
			//Get the attribute's value
			//type name only(whether is the unique value in the DB-Table)
			Data d = new Data(in.next(),in.next(),in.nextBoolean());
			//It will Set the field of the class to the DB-table name
			d.setDatabaseName(s+"_"+d.getDatabaseName());
			//add to the field list
			list.add(d);
		}
	}
	
	/**
	 * Set all the VARchar
	 */
	public void set(){
		
		/**
		 * Set the package
		 */
		String s = var.getValue("$PACKAGE");
		s=s.replace('.', '/');
		//Set the url in the ALL xml and properties
		var.setValue("$PKGURL",s );
		
		s="";
		for(int i=0;i<list.size();i++){
			s+="<result property=\""+ (list.get(i)).getName()+"\" column=\""+(list.get(i)).getDatabaseName()+"\"/>\n";
		}
		var.setValue("$RESULTMAP", s);
		
		s="";
		for(int i=0;i<list.size()-1;i++){
			s+=(list.get(i)).getDatabaseName()+" as "+(list.get(i)).getName()+",\n";
		}
		s+=(list.get(list.size()-1)).getDatabaseName()+" as "+(list.get(list.size()-1)).getName()+"\n";
		var.setValue("$SELECTONE",s);
		
		s="(\n";
		for(int i=0;i<list.size()-1;i++){
			s+=(list.get(i)).getDatabaseName()+",\n";
		}
		s+=(list.get(list.size()-1)).getDatabaseName()+")\n";
		s+="values(\n";
		for(int i=0;i<list.size()-1;i++){
			s+="#"+(list.get(i)).getName()+"#,";
		}
		s+="#"+(list.get(list.size()-1)).getName()+"#)";
		var.setValue("$INSERT", s);
		
		s="";
		for(int i=1;i<list.size()-1;i++){
			s+=(list.get(i)).getDatabaseName()+" = #"+(list.get(i)).getName()+"#,\n";
		}
		s+=(list.get(list.size()-1)).getDatabaseName()+" = #"+(list.get(list.size()-1)).getName()+"#\n";
		var.setValue("$UPDATE", s);
	}
	
	public String toString(){
		String s="";
		s=var.toString();
		return s;
	}

}
